<template>
  <div class="main">
    <div class="form">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="用户名" prop="name">
        <el-input v-model="ruleForm.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="link" @click="goLogin()">
      <el-link type="info">已有账号？去登陆吧</el-link>
    </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import url from "@/serviceAPI.config.js"
import { Toast } from "vant"
  export default {
    name: "Register",
    data() {
      var validatePass = (rule, value, callback) => {
        if (value.length < 6) {
          callback(new Error('用户名长度不能小于6位'));
        } else {
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value.length < 6) {
          callback(new Error('密码长度不能小于6位'));
        }else {
          callback();
        }
      };
      return {
        ruleForm: {
          name: '',
          pass: '',
        },
        rules: {
          name: [
            { validator: validatePass, trigger: 'blur' }
          ],
          pass: [
            { validator: validatePass2, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      goLogin(){
        this.$router.push("/login");
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            // 表单检验成功 可以登录
            this.axiosRegisterUser();
          } else {
            this.$message.error("请正确填写表单");
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      goBack() {
        this.$router.go(-1);
      },
      registerAction() {
        this.checkForm() && this.axiosRegisterUser();
      },
      axiosRegisterUser() {
        this.openLoading = true;
        axios({
          url: url.registerUser,
          method: "post",
          data: {
            userName: this.ruleForm.name,
            passWord: this.ruleForm.pass
          }
        })
        .then(res => {
          this.openLoading = false;
          if(res.data.code === 200) {
            this.$message.success(res.data.message);
            console.log(res);
            this.$router.push("/login");
          }else {
            this.$message.error(res.data.message);
            console.log(res);
          }
        })
        .catch(err => {
          this.openLoading = false;
          console.log(err);
        })
      },
      // 验证表单数据正确与否
      checkForm() {
        let isOk = true;
        if(this.userName.length < 5) {
          isOk = false;
          this.userNameErrorMsg = "用户名长度不能低于5位";
        }else {
          this.userNameErrorMsg = "";
        }
        if(this.passWord.length < 6) {
          isOk = false;
          this.passWordErrorMsg = "密码长度不能低于6位";
        }else {
          this.passWordErrorMsg = "";
        }
        return isOk;
      }
    },
  }
</script>

<style scoped>
  .main {
    height: 752px;
    width: 100%;
    background: url("../../assets/image/reBG.jpg");
    background-size: cover;
    background-color: red;
  }
  .form {
    width: 300px;
    height: 200px;
    position: absolute;
    right: 200px;
    top: 200px;
    background-color: #fff;
    padding-right: 56px;
    padding-top: 42px;
    border-radius: 10px;
  }
  .register-panel {
    width: 300px;
    border-radius: 0.3rem;
    margin: 300px auto;
    /* padding-bottom: 15rem; */
  }
  .register div {
    border-radius: 5rem;
  }
  .register-botton {
    padding-top: .9rem;
  }
  .link {
    position: absolute;
    left: 217px;
    bottom: 10px;
  }
</style>